﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Partial Class Control_WhatCookToDay_ShowVideoFormula
    Inherits System.Web.UI.UserControl
    Private strPlayerUrl As String
    Private strFileUrl As String
    Private strImageUrl As String

#Region "Property"
    Public Property PlayerUrl() As String
        Get
            Return strPlayerUrl
        End Get
        Set(ByVal Value As String)
            strPlayerUrl = Value
        End Set
    End Property
    Public Property FileUrl() As String
        Get
            Return strFileUrl
        End Get
        Set(ByVal Value As String)
            strFileUrl = Value
        End Set
    End Property
    Public Property ImageUrl() As String
        Get
            Return strImageUrl
        End Get
        Set(ByVal Value As String)
            strImageUrl = Value
        End Set
    End Property
#End Region
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Page.RouteData.Values("id") Is Nothing Then
                hidID.Value = Page.RouteData.Values("id")
                ShowVideo()
            End If
        End If
    End Sub

    Sub ShowVideo()
        Using data As New VSEntities
            Dim p As formula = (From q In data.formulae Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                'Hien thi clip
                If p.IsYoutube = True Then
                    Dim baseUrl As String = Request.Url.GetLeftPart(UriPartial.Authority)
                    divEmbededVideo.Visible = False
                    divFileYoutube.Visible = True
                    FileUrl() = SplitVideoYoutube(p.URL)
                    ImageUrl() = baseUrl + ResolveUrl_Video(p.Image)
                Else
                    divEmbededVideo.Visible = True
                    divFileYoutube.Visible = False
                    Dim baseUrl As String = Request.Url.GetLeftPart(UriPartial.Authority)
                    PlayerUrl() = baseUrl + ResolveUrl_Video("~/jwplayer/player.swf")
                    FileUrl() = baseUrl + ResolveUrl_Video(p.URL)
                    ImageUrl() = baseUrl + ResolveUrl_Video(p.Image)
                End If
            End If
        End Using
    End Sub
    Protected Function SplitVideoYoutube(ByVal strUrl As String) As String
        Dim intStart As Integer = strUrl.IndexOf("=")
        Dim intEnd As Integer = strUrl.IndexOf("&")
        Dim scr As String = strUrl.Substring(intStart + 1, strUrl.Length - intStart - 1)
        Return scr
    End Function
    Public Function ResolveUrl_Video(ByVal url As String) As String
        If url Is Nothing Then
            Return ""
        End If
        If url.Length = 0 Then
            Return ""
        End If

        ' there is no ~ in the first character position, just return the url 
        If url(0) <> "~"c Then
            Return url
        End If

        Dim applicationPath As String = HttpContext.Current.Request.ApplicationPath

        ' there is just the ~ in the URL, return the applicatonPath 
        If url.Length = 1 Then
            Return applicationPath
        End If

        ' assume url looks like ~somePage 
        Dim indexOfUrl As Integer = 1

        ' determine the middle character 
        Dim midPath As String = If((applicationPath.Length > 1), "/", String.Empty)

        ' if url looks like ~/ or ~\ change the indexOfUrl to 2 
        If url(1) = "/"c OrElse url(1) = "\"c Then
            indexOfUrl = 2
        End If

        Return applicationPath & midPath & url.Substring(indexOfUrl)
    End Function
#End Region
End Class
